package com.yl.ifanr;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.util.Log;
import android.content.Intent;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

import com.yl.ifanr.service.BaseService;
import com.yl.ifanr.service.RssServiceimpl;
import com.yl.util.NetDataLoader;
import com.yl.util.NetDataLoader.DataCallback;
import com.yl.ifanr.R;
import com.yl.ifanr.db.ItemlistAdapter;
import com.yl.ifanr.db.RssDaoimpl;
import com.yl.ifanr.db.RssItemBean;

public class ActivityMain extends Activity implements OnItemClickListener{
	public final String TAG = "ActivityMain";
	private ImageButton reflash;
	private List<RssItemBean> itemList;
	private ListView listView;
	private RssDaoimpl rssDaoimpl;
	private RelativeLayout mainLayout;
	private LoadingUI loadingUI;
	private RelativeLayout.LayoutParams pbLP;
	private NetDataLoader netDataLoader;//访问网络线程池
	private RssServiceimpl rssServiceimpl;

/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    Log.d(TAG, "onCreate");
    setContentView(R.layout.main);
    
    netDataLoader = new NetDataLoader();
	reflash = (ImageButton)findViewById(R.id.flash);
	reflash.setOnClickListener(flashContent);
	
	listView = (ListView) findViewById(R.id.rss_list_item); 
    listView.setOnItemClickListener(this);
    
    rssDaoimpl = new RssDaoimpl(this);
	showListView();
}
/**
 * 显示列表
 */
private void showListView(){
	itemList = rssDaoimpl.getAllItem();
	ItemlistAdapter itemlistAdapter = new ItemlistAdapter(this,itemList);
	listView.setAdapter(itemlistAdapter);
}
/**
 * 刷新按钮响应事件
 */
private ImageButton.OnClickListener flashContent= new ImageButton.OnClickListener(){
	public void onClick(View v){
		//显示进度条
		mainLayout = (RelativeLayout)findViewById(R.id.ifan_center);
		loadingUI = new LoadingUI(ActivityMain.this, "加载中...");
		pbLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		pbLP.addRule(RelativeLayout.CENTER_IN_PARENT);
		mainLayout.addView(loadingUI, pbLP);
		
		//更新数据
    	rssServiceimpl = new RssServiceimpl(ActivityMain.this);
		//更新完数据后的回调
		DataCallback dataCallback = new DataCallback() {
			@Override
			public void dataLoaded(BaseService baseService) {
				showListView();
				mainLayout.removeView(loadingUI);
			}
			
		};
		 //先更新数据
        netDataLoader.loadData(rssServiceimpl, dataCallback);
	}
};
/**
 * 文章列表被点击事件处理程序
 */
 public void onItemClick(AdapterView parent, View v, int position, long id)
 {
	 Intent intent = new Intent(this,ActivityShowDesc.class);
     
     RssItemBean rssItemBean = (RssItemBean)v.getTag();
     intent.putExtra("title",rssItemBean.getTitle());
     intent.putExtra("desc",rssItemBean.getDescription());
     intent.putExtra("content",rssItemBean.getContent());
     intent.putExtra("link",rssItemBean.getLink());
     intent.putExtra("pubdate",rssItemBean.getPubdate());
     
     startActivity(intent);
 }
/**
 * 退出程序
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
	switch (keyCode) {
	case KeyEvent.KEYCODE_BACK:
		new AlertDialog.Builder(this).setTitle("提示").setCancelable(false)// 设置不能通过“后退”按钮关闭对话框
				.setMessage("确定退出客户端?").setPositiveButton("确认",new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
								finish();
								System.exit(0);
							}
						}).setNegativeButton("取消",new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int id) {
								dialog.cancel();
							}
						}).show();// 显示对话框
		return true;
	}
	return false;
}
}